﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace BaseTool
{
    public class PortHelper
    {
        /// <summary>
        /// 读取设备串口
        /// </summary>
        /// <returns></returns>
        public static string[] GetPorts()
        {
            try
            {
                return SerialPort.GetPortNames();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设置波特率
        /// </summary>
        /// <param name="serialPort"></param>
        /// <param name="port"></param>
        /// <param name="baudRate"></param>
        public static void InitPort(SerialPort serialPort, string port, int baudRate)
        {
            try
            {
                serialPort.PortName = port;
                serialPort.BaudRate = baudRate;
                serialPort.Parity = Parity.None;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送字符串
        /// </summary>
        /// <param name="port"></param>
        /// <param name="data"></param>
        public static void SendData(SerialPort port, string data)
        {
            try
            {
                port.Write(data);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送字节数组
        /// </summary>
        /// <param name="port"></param>
        /// <param name="data"></param>
        public static void SendData(SerialPort port, byte[] data)
        {
            try
            {
                port.Write(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 读取返回字符串
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="delay">返回超时</param>
        /// <param name="wait">返回等待</param>
        /// <returns></returns>
        public static string GetData(SerialPort port, int delay, int wait)
        {
            try
            {
                string reData = "";
                if (wait != 0)
                {
                    CommonHelper.Delay(wait);
                }
                while (true)
                {
                    if (port.BytesToRead > 2 || delay <= 0)
                    {
                        byte[] recData = new byte[port.BytesToRead];
                        port.Read(recData, 0, recData.Length);
                        reData = Encoding.Default.GetString(recData);
                        break;
                    }
                    CommonHelper.Delay(100);
                    delay -= 100;
                }
                return reData;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 读取返回字节
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="delay">返回超时</param>
        /// <param name="wait">返回等待</param>
        /// <returns></returns>
        public static byte[] GetByteData(SerialPort port, int delay, int wait)
        {
            try
            {
                byte[] recData = new byte[] { };
                if (wait != 0)
                {
                    CommonHelper.Delay(wait);
                }
                while (true)
                {
                    if (port.BytesToRead > 2 || delay <= 0)
                    {
                        recData = new byte[port.BytesToRead];
                        port.Read(recData, 0, recData.Length);
                        break;
                    }
                    CommonHelper.Delay(100);
                    delay -= 100;
                }
                return recData;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
